package com.myapp.ruyixing.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.http.ScoreChangeDetailParser.ScoreChangeDetail;
import com.myapp.ruyixing.http.ScoreChangeDetailParser.ScoreChangeDetailInfo;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.BitmapLruCache;
import com.myapp.ruyixing.utils.ImageUtils;
import com.myapp.ruyixing.utils.SafeUtils;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;

public class ScoreChangeDetailActivity extends BaseActivity {

	private String goodsid;

	private View mLoadingView;

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			ScoreChangeDetail scd = (ScoreChangeDetail) o;

			if (scd != null) {
				showView(scd);
			}

			// hideLoading();
		}

		@Override
		public void onFailure(Throwable throwable) {
			Toast.makeText(getApplicationContext(), "加载失败", Toast.LENGTH_SHORT)
					.show();

			// hideLoading();
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_score_change_detail);

		goodsid = getIntent().getStringExtra("goodsid");

		mLoadingView = findViewById(R.id.my_loading);
		// showLoading();

		RYHttpClient.getScoreChangeDetail(this, mHttpResponseHandler, goodsid);

		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});
	}

	private void showLoading() {
		mLoadingView.setVisibility(View.VISIBLE);
	}

	private void hideLoading() {
		mLoadingView.setVisibility(View.GONE);
	}

	private void showView(ScoreChangeDetail data) {
		RequestQueue mQueue = Volley.newRequestQueue(this);
		ImageLoader mImageLoader = new ImageLoader(mQueue, new BitmapLruCache());

		((TextView) findViewById(R.id.title)).setText(data.title);
		((TextView) findViewById(R.id.remain))
				.setText("剩余" + data.remain + "件");
		((ImageView) findViewById(R.id.score)).setImageLevel(SafeUtils
				.safeString2int(data.score));
		((NetworkImageView) findViewById(R.id.image)).setImageUrl(data.thumb,
				mImageLoader);
		((TextView) findViewById(R.id.changescore)).setText("兑换积分："
				+ data.points);
		((TextView) findViewById(R.id.price)).setText("参考价格：￥" + data.price);
		((TextView) findViewById(R.id.changecode)).setText("兑换编码："
				+ data.codenumber);
		((TextView) findViewById(R.id.changetime)).setText("兑换次数："
				+ data.changetimes);
		
		findViewById(R.id.exchange).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivity(new Intent(
						ScoreChangeDetailActivity.this,
						ScoreOrderFormActivity.class).putExtra(
						"goodsid", goodsid));
			}
		});

		for (ScoreChangeDetailInfo item : data.items) {
			if (item.type.equals("goodsdetail")) {
				ImageUtils.displayImage(
						item.content,
						(ImageView) findViewById(R.id.picture),
						new DisplayImageOptions.Builder()
								.imageScaleType(
										ImageScaleType.EXACTLY_STRETCHED)
								.cacheOnDisc().build());
			}

			else if (item.type.equals("review")) {
				findViewById(R.id.view_remark).setVisibility(View.VISIBLE);

				ViewGroup child = (ViewGroup) findViewById(R.id.view_remark);
				((TextView) child.findViewById(R.id.text_title))
						.setText(item.title + "(共" + item.reviewnum + "条)");
				((TextView) child.findViewById(R.id.text_name))
						.setText(item.username);
				((TextView) child.findViewById(R.id.text_content))
						.setText(item.content);
				((TextView) child.findViewById(R.id.text_time))
						.setText(item.date);
				if (item.score >= 0 && item.score <= 5) {
					((ImageView) child.findViewById(R.id.score))
							.setImageLevel(item.score);
				}

				child.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						startActivity(new Intent(
								ScoreChangeDetailActivity.this,
								ScoreRemarkListActivity.class).putExtra(
								"goodsid", goodsid));
					}
				});
			}

			else if (item.type.equals("changerecord")) {
				findViewById(R.id.view_exchange).setVisibility(View.VISIBLE);

				ViewGroup child = (ViewGroup) findViewById(R.id.view_exchange);
				((TextView) child.findViewById(R.id.text_title))
						.setText(item.title + "(共" + item.recordnum + "条)");
				((TextView) child.findViewById(R.id.text_name))
						.setText(item.username + " 兑换了" + item.changenum + "件");
				((TextView) child.findViewById(R.id.text_time))
						.setText(item.date);

				child.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						startActivity(new Intent(
								ScoreChangeDetailActivity.this,
								ScoreExchangeListActivity.class).putExtra(
								"goodsid", goodsid));
					}
				});
			}
		}
	}
}
